//
// C++ Implementation: activity
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activity.h"

int Activity::visibleFields = 10;

Activity::Activity ()
{
    date = QDate::currentDate();
    time = QTime ( 12, 0, 0, 0 );
    duration = QTime ( 0, 0, 0, 0 );
    distance = 0;
    effort = 0;
    location = "Location";
    route = "Route";
    notes = "Notes";
    splits = "Splits";
    type = "Run";
    id = 0;
}

Activity::Activity ( const Activity& act )
{
    date = act.date;
    time = act.time;
    duration = act.duration;
    distance = act.distance;
    location = act.location;
    type = act.type;
    route = act.route;
    notes = act.notes;
    splits = act.splits;
    effort = act.effort;
    id = act.id;
}

Activity& Activity::operator= ( const Activity& act )
{
    date = act.date;
    time = act.time;
    duration = act.duration;
    distance = act.distance;
    location = act.location;
    type = act.type;
    route = act.route;
    notes = act.notes;
    splits = act.splits;
    effort = act.effort;
    id = act.id;

    return *this;
}


Activity::~Activity()
{
}

/**
* Accessor.
* @return The queried value.
*/
QDate   Activity::getDate()
{
    return date;
}

/**
* Accessor.
* @return The queried value.
*/
QTime   Activity::getTime()
{
    return time;
}

/**
* Accessor.
* @return The queried value.
*/
QTime   Activity::getDuration()
{
    return duration;
}

/**
* Accessor.
* @return The queried value.
*/
float   Activity::getDistance()
{
    return distance;
}

QString Activity::getType()
{
    return type;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getLocation()
{
    return location;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getRoute()
{
    return route;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getNotes()
{
    return notes;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getSplits()
{
    return splits;
}

/**
* Accessor.
* @return The queried value.
*/
int     Activity::getEffort()
{
    return effort;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setDate ( QDate date )
{
    this->date = date;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setTime ( QTime var )
{
    this->time = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setDuration ( QTime var )
{
    this->duration = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setDistance ( float var )
{
    this->distance = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setLocation ( QString var )
{
    this->location = var;
}

void Activity::setType ( QString type )
{
    this->type = type;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setRoute ( QString var )
{
    this->route = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setNotes ( QString var )
{
    this->notes = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setSplits ( QString var )
{
    this->splits = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setEffort ( int var )
{
    this->effort = var;
}


int Activity::getId()
{
    return id;
}

void Activity::setId ( int i )
{
    id = i;
}

/**
* Mutator to set the active database.
* @param db The database to which to add.
*/
void Activity::setDB ( QSqlDatabase* db )
{
    this->db = db;
}


/**
* Insert this activity into the database's records.
* @param db The database to add to.
* @return True on successful addition.
*/
bool Activity::insert ( QSqlDatabase* db )
{
    QSqlQuery query ( *db );

    if ( id == 0 ) // this is a new activity then
    {
        query.prepare ( "INSERT INTO logTable "
                        "("
                        "date, time, duration, distance, type,"
                        " location, route, notes, splits, effort"
                        ")"
                        "VALUES"
                        "("
                        ":date, :time, :duration, :distance, :type, "
                        ":location, :route, :notes, :splits, :effort"
                        ")" );
        query.bindValue ( ":date",      date );
        query.bindValue ( ":time",      time );
        query.bindValue ( ":duration",  duration );
        query.bindValue ( ":distance",  distance );
        query.bindValue ( ":type",      type );
        query.bindValue ( ":location",  location );
        query.bindValue ( ":route",     route );
        query.bindValue ( ":notes",     notes );
        query.bindValue ( ":splits",    splits );
        query.bindValue ( ":effort",    effort );
        if ( !query.exec() )
        {
            qWarning ( "failed to insert activity into database" );
            return false;
        }
        return true;
    }
    else    // else were editing a previous activity
    {
        query.prepare ( "UPDATE logTable SET "
                        "date=:date, time=:time, duration=:duration, distance=:distance, type=:type, "
                        "location=:location, route=:route, notes=:notes, splits=:splits, effort=:effort "
                        "WHERE id=:id"
                      );
        query.bindValue ( ":id",        id );
        query.bindValue ( ":date",      date );
        query.bindValue ( ":time",      time );
        query.bindValue ( ":duration",  duration );
        query.bindValue ( ":distance",  distance );
        query.bindValue ( ":type",      type );
        query.bindValue ( ":location",  location );
        query.bindValue ( ":route",     route );
        query.bindValue ( ":notes",     notes );
        query.bindValue ( ":splits",    splits );
        query.bindValue ( ":effort",    effort );
        if ( !query.exec() )
        {
            qWarning ( "failed to update activity in database" );
            qDebug() << query.lastQuery();
            qDebug() << query.lastError().text();
            return false;
        }
        return true;
    }
}

/**
* Overloaded insert method.
* @return True on successful insert.
*/
bool Activity::insert ()
{
    return this->insert ( db );
}



